const baseUrl = 'https://applet.youjia.top/index.php/applet/';
// const baseUrl = 'http://youjia.test/applet/';

App({
    onShow() {},
    setTitle(title) {
        wx.setNavigationBarTitle({
            title: title
        });
    },
    switchTab(path) {
        wx.switchTab({
            url: path,
        });
    },
    navigateTo(path) {
        wx.navigateTo({
            url: path,
        });
    },
    tips(tip, time = 2000, type = 'none') {
        wx.showToast({
            title: tip,
            icon: type,
            mask: true,
            duration: time
        });
    },
    loading(tip = '加载中...') {
        wx.showLoading({
            title: tip,
            mask: true,
        })
    },
    wechatPay(option) {
        let _this = this;
        this.HttpsRequest(option.url, 'POST', option.data).then(ret => {
            if (ret) {
                let payment = ret.data.payment;
                let msg = ret.data.msg;
                wx.requestPayment({
                    timeStamp: payment.timeStamp,
                    nonceStr: payment.nonceStr,
                    package: payment.package,
                    signType: payment.signType,
                    paySign: payment.paySign,
                    success(res) {
                        _this.tips(msg.success);
                        option.callback();
                    },
                    fail(res) {
                        _this.tips(msg.error);
                        option.callback();
                    },
                    complete(res) {}
                })
            }
        })
    },
    checkLogin() {
        return wx.getStorageSync('token');
        // if(token && key) return toekn;
        // this.navigateTo('/pages/login/login')
    },
    HttpsRequest(url, method = 'GET', data = {}, headers = {}) {
        let that = this;
        return new Promise(function(resolve, reject) {
            wx.request({
                url: `${baseUrl}${url}`,
                method: method,
                data: data,
                header: {
                    'Authorization': that.checkLogin(),
                },
                success: function(res) {
                    //HTTPS请求失败;
                    if (res.statusCode != 200) {
                        that.tips('请求失败')
                        return false;
                    }
                    //API逻辑失败;
                    if (res.data.status == that.globalData.error) {
                        that.tips(res.data.msg)
                        resolve(false);
                    }
                    //尚未登陆
                    if (res.data.code == 401) {
                        that.navigateTo('/pages/login/login');
                        return;
                    }
                    resolve(res.data);

                },
                fail: function(res) {
                    // fail调用接口失败
                    reject({
                        error: '网络错误',
                        code: 0
                    });
                },
                complete: function(res) {
                    // complete
                }
            })
        })
    },
    onLaunch(e) {
        // console.log(e);
    },


    toLogin(param = {}) {
        let _this = this;
        _this.tips('请登陆');
        let query = '';
        for (let i in param) {
            if (param[i]) {
                if ('' != query) {
                    query += '&' + i + '=' + param[i];
                } else {
                    query += i + '=' + param[i];
                }
            }
        }
        setTimeout(function() {
            _this.navigateTo('/pages/login/login?' + query);
        }, 1900)
    },
    globalData: {
        success: 200,
        error: 500,
    }
})